/*
 * ChangeGoodsCountDialog.java
 * business
 *
 * Created by ChenTao on 2017/1/18.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.UIUtils;

/**
 * 修改商品数量对话框
 */
public class ChangeGoodsCountDialog extends Dialog implements View.OnClickListener, TextWatcher {

    private TextView mTvTitle;

    private ImageView mTvSubtract;

    private EditText mEtCount;

    private ImageView mTvAdd;

    private Button mBtnCancel;

    private Button mBtnConfirm;

    private RelativeLayout mRlytClose;

    private int mGoodsCount;

    private int mMaxGoodsCount;

    private int mMinGoodsCount = 1;

    private String mOverStockTipHint;

    private OnConfirmDialogGoodsCountListener mListener;

    public ChangeGoodsCountDialog(Context context) {
        super(context, R.style.ThemeNoTitleDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_change_goods_count);
        initViews();
    }

    private void initViews() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvSubtract = (ImageView) findViewById(R.id.tv_subtract);
        mEtCount = (EditText) findViewById(R.id.et_count);
        mTvAdd = (ImageView) findViewById(R.id.tv_add);
        mBtnCancel = (Button) findViewById(R.id.btn_shopping_cart);
        mBtnConfirm = (Button) findViewById(R.id.btn_buy_now);
        mRlytClose = (RelativeLayout) findViewById(R.id.rlyt_close);

        mRlytClose.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
        mTvAdd.setOnClickListener(this);
        mTvSubtract.setOnClickListener(this);
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 将商品数量传回
                dismiss();
                if (mListener != null) {
                    mListener.onConfirmDialogCount(getGoodsCount());
                }
            }
        });
    }

    /**
     * 初始化商品数量
     *
     * @param goodsCount    商品数量
     * @param maxGoodsCount 最大数量
     */
    public void initGoodsCount(int goodsCount, int maxGoodsCount, int minGoodsCount) {
        mGoodsCount = goodsCount;
        mMinGoodsCount = minGoodsCount < 1 ? 1 : minGoodsCount;
        if (goodsCount <= mMinGoodsCount) {
            setSubtractButtonEnable(false);
        }

        mMaxGoodsCount = maxGoodsCount;
        mEtCount.setText(goodsCount + "");

        mEtCount.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_shopping_cart:
                // 取消
                dismiss();
                break;
            case R.id.tv_subtract:
                // 减少
                mGoodsCount--;
                setGoodsCount(mGoodsCount);
                break;
            case R.id.tv_add:
                // 增加
                mGoodsCount++;
                setGoodsCount(mGoodsCount);
                break;
            case R.id.rlyt_close:
                // 隐藏
                dismiss();
                break;
        }
    }

    private String getEditTextContent() {
        return mEtCount.getText().toString().trim();
    }

    public void setSubtractButtonEnable(boolean enable) {
        mTvSubtract.setEnabled(enable);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {

        String inputText = s.toString();

        if (!TextUtils.isEmpty(inputText)) {
            int inputNumber = Integer.parseInt(inputText);
            if (inputNumber != 0) {
                // 输入的不是0
                mGoodsCount = inputNumber;
                // 设置减号可用/不可用
                setSubtractButtonEnable(mGoodsCount > mMinGoodsCount);
            } else {
                // 输入内容为0时，设置为最少起订量
                setGoodsCount(mMinGoodsCount);
            }
        } else {
            // 输入的内容为空
            mGoodsCount = mMinGoodsCount;
        }

        if (mGoodsCount > mMaxGoodsCount) {
            // 超过最大值时，需要将显示设置为最大值
            setGoodsCount(mMaxGoodsCount);
            showOverStockTipToast();
        }
    }

    /**
     * 显示设置的商品数量超过库存时的Toast提示
     */
    private void showOverStockTipToast() {
        if (TextUtils.isEmpty(mOverStockTipHint)) {
            UIUtils.showToast(UIUtils.getString(R.string.goods_stock_not_enough_now, mMaxGoodsCount));
        } else {
            UIUtils.showToast(mOverStockTipHint);
        }
    }

    private int getGoodsCount() {
        return mGoodsCount;
    }

    private void setGoodsCount(int goodsCount) {
        if (goodsCount > mMaxGoodsCount) {
            showOverStockTipToast();
            mGoodsCount = mMaxGoodsCount;
            return;
        }

        if (goodsCount < mMinGoodsCount) {
            // 设置最少数量为最少起订量
            mGoodsCount = mMinGoodsCount;
            setSubtractButtonEnable(false);
            UIUtils.showToast(UIUtils.getString(R.string.min_number, mMinGoodsCount));
        } else {
            setSubtractButtonEnable(true);
        }

        mGoodsCount = goodsCount;
        mEtCount.setText(mGoodsCount + "");
        mEtCount.setSelection(getEditTextContent().length());
    }

    /**
     * 设置修改商品数量对话框的标题
     */
    public void setDialogTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            mTvTitle.setText(UIUtils.getString(R.string.change_buy_count));
        } else {
            mTvTitle.setText(title);
        }
    }

    /**
     * 设置超过库存时的Toast提示内容
     */
    public void setOverStockTipHint(String overStockTipHint) {
        mOverStockTipHint = overStockTipHint;
    }

    public void setOnConfirmDialogGoodsCountListener(OnConfirmDialogGoodsCountListener listener) {
        this.mListener = listener;
    }

    /**
     * 当点击对话框确认按钮的回掉
     */
    public interface OnConfirmDialogGoodsCountListener {

        void onConfirmDialogCount(int dialogGoodsCount);
    }
}
